const {
  defineConfig
} = require('@vue/cli-service')
const PrerenderSPAPlugin = require('@dreysolano/prerender-spa-plugin');
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
const path = require("path");

function resolve(dir) {
  return path.join(__dirname, dir)
}
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const isProduction = process.env.NODE_ENV === 'production';
const port = process.env.port || process.env.npm_config_port || 8080 // dev port
module.exports = {
  transpileDependencies: true,
  publicPath: "/",
  outputDir: 'dist',
  // assetsDir: "static",
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    open: false,
  },
  configureWebpack: config => {
    if (!isProduction) return {};
    return {
      plugins: [
        new PrerenderSPAPlugin({
          staticDir: path.join(__dirname, 'dist'),
          routes: ['/', '/digitalEmployee', '/bovineVision', '/digitalMan','/coreTechnology','/channel','/companyIntroduction','/news','/aboutUs'],
          indexPath: path.join(__dirname, 'dist/index.html'),
          renderer: new Renderer({
            inject: {
              foo: 'bar'
            },
            // headless: false,
            headless: true,
            renderAfterTime: 10000,
            maxConcurrentRoutes: 20, //打包页面的最大数
            renderAfterDocumentEvent: 'render-event',
            navigationParams: {
              timeout: 0
            }
          }),
        })
      ],
    };
  },
  chainWebpack: (config) => {
    config.plugins.delete('prefetch')
    // 移除 preload 插件，避免加载多余的资源
    config.plugins.delete('preload');
    //图形化信息，分析哪些文件过大
    //图片压缩处理
    // config.module.rule('images')
    // .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
    // .use('image-webpack-loader')
    // .loader('image-webpack-loader')
    // .options({ bypassOnDebug: true })

    config.resolve.alias.set('@', resolve('src'))
    config.resolve.alias.set('@assets', resolve('src/assets'))
    config.resolve.alias.set('@views', resolve('src/views'))
    config.resolve.alias.set('@utils', resolve('src/utils'))
    config.resolve.alias.set('@style', resolve('src/style'))
    config.resolve.alias.set('@api', resolve('src/api'))
    config.resolve.alias.set('@store', resolve('src/store'))
    config.resolve.alias.set('@router', resolve('src/router'))
    config.resolve.alias.set('@http', resolve('src/http'))
    config.resolve.alias.set('@static', resolve('src/static'))
    config.resolve.alias.set('@components', resolve('src/components'))
  }
}
